package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.dao.CatDao;
import com.jx.daoimpl.CatDaoImpl;
import com.jx.model.Category;
import com.jx.service.CatService;
import com.jx.servicrimpl.CatServiceImpl;

import net.sf.json.JSONObject;

public class ShowCatController extends HttpServlet{
		//定义CatService类型的引用catservice指向CatServiceImpl对象，用以调用service层的显示所有商品分类信息的方法
		CatService catservice = new CatServiceImpl();
		//定义CatDao类型的引用catdao指向CatDaoImpl对象，用以调用dao层返回商品分类对象数组的方法
		CatDao catdao = new CatDaoImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
			//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建session对象
			HttpSession session = req.getSession();
			//创建流对象
			PrintWriter pw = resp.getWriter();
			//获取前台传来的值
			int page = Integer.parseInt(req.getParameter("page"));
			int pageSize = Integer.parseInt(req.getParameter("rows"));
			int start = (page-1)*pageSize;
			String cname = req.getParameter("catname");
			JSONObject cs = new JSONObject();
			session.setAttribute("category",catdao.showCatrgory(start, pageSize));
			if (cname==null) {
				cs = catservice.showCat(start, pageSize);
				pw.write(cs.toString());
			}else {
				cs = catservice.showCat(cname, start, pageSize);
				pw.write(cs.toString());;
			}

			pw.flush();
			pw.close();
		}
		
}
